﻿/// <reference path="utils.js" />
'use strict';

define(function () {
    var View = Backbone.View.extend({
        rendered: false,
        visible: false,

        initialize: function (options) {
            this._loadTemplate();

            return this;
        },

        beforeShow: function () {
            return true;
        },
        delegateModelEvents: function () { return this; },
        destroy: function () {
            this.undelegateEvents()
                .undelegateModelEvents();

            this.$el.empty()
                    .remove();

            /* Allow a future newer first render. */
            this.rendered = false;
        },
        hide: function () {
            this.$el.hide();
            this.undelegateModelEvents();

            this.visible = false;
        },
        _loadTemplate: function () {
            var that = this
                , fn_success = function (response) {
                    that.template = Handlebars.compile(response);
                };

            $.ajax({
                url: sprintf('%s/%s/%s.template.html', App.config.screensFolderUrl, this.name, this.name),
                async: false,
                dataType: 'html',
                success: fn_success
            });

            return this;
        },
        render: function () {
            this.$el.append(this.template(_.extend(
                this.model,
                {
                    id: this.id,
                })));


            return this;
        },
        show: function (data) {
            if (this.visible == false)
                this.delegateModelEvents();

            //if ((data) && (data.length) && (data[0]))
            //    this.model.set(data);

            this.$el.show();

            this.visible = true;
        },
        undelegateModelEvents: function () {
            this.stopListening();

            return this;
        },
    });

    return View;
});